package com.exp.base.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.LocaleResolver;

import com.google.common.collect.Lists;

/**
 * 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
	
    private static final char SEPARATOR = '_';
    private static final String CHARSET_NAME = "UTF-8";
    
    
	/**
	 * 去除HTML标签方法
	 */
	public static String replaceHtml(String html) {
		if (isBlank(html)){
			return "";
		}
		String regEx = "<.+?>";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(html);
		String s = m.replaceAll("");
		return s;
	}
	
	/**
	 * 替换为手机识别的HTML，去掉样式及属性，保留回车。
	 * @param txt
	 * @return
	 */
	public static String toHtml(String txt){
		if (txt == null){
			return "";
		}
		return replace(replace(StringEscapeUtils.escapeHtml4(txt), "\n", "<br/>"), "\t", "&nbsp; &nbsp; ");
	}

	
	/**
	 * 驼峰命名法工具
	 * @return
	 * 		toCamelCase("hello_world") == "helloWorld" 
	 * 		toCapitalizeCamelCase("hello_world") == "HelloWorld"
	 * 		toUnderScoreCase("helloWorld") = "hello_world"
	 */
    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
    	boolean find_ = false;   //发现word中的_
    	boolean wordstart=false; //去除_开头的word开始
    	boolean wordHead=false; //是否处理了头
        for(int i=0;i<s.length();i++){
        	char c = s.charAt(i);
        	if(!wordstart){
        		if(c!='_'){
        			wordstart=true;
        		}else{
        			continue;
        		}
        	}
        	if(!wordHead){
        		char _c = Character.toLowerCase(c);
        		sb.append(_c);
        		wordHead=true;
        	}else{
        		if(find_){
            		char _c = Character.toUpperCase(c);
            		sb.append(_c);
            		find_=false;
        		}else if(c=='_'){
        			find_=true;
        		}else{
            		sb.append(c);
        		}
        	}
        }

/*        s = s.toLowerCase();

        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c == SEPARATOR) {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }
*/
        return sb.toString();
    }

    /**
	 * 驼峰命名法工具
	 * @return
	 * 		toCamelCase("hello_world") == "helloWorld" 
	 * 		toCapitalizeCamelCase("hello_world") == "HelloWorld"
	 * 		toUnderScoreCase("helloWorld") = "hello_world"
	 */
    public static String toCapitalizeCamelCase(String s) {
        if (s == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
    	boolean find_ = false;   //发现word中的_
    	boolean wordstart=false; //去除_开头的word开始
    	boolean wordHead=false; //是否处理了头
        for(int i=0;i<s.length();i++){
        	char c = s.charAt(i);
        	if(!wordstart){
        		if(c!='_'){
        			wordstart=true;
        		}else{
        			continue;
        		}
        	}
        	if(!wordHead){
        		char _c = Character.toUpperCase(c);
        		sb.append(_c);
        		wordHead=true;
        	}else{
        		if(find_){
            		char _c = Character.toUpperCase(c);
            		sb.append(_c);
            		find_=false;
        		}else if(c=='_'){
        			find_=true;
        		}else{
            		sb.append(c);
        		}
        	}
        }
        return sb.toString();
//        s = toCamelCase(s);
//        return s.substring(0, 1).toUpperCase() + s.substring(1);
    }
    
    /**
	 * 下划线命名法工具
	 * @return
	 * 		toCamelCase("hello_world") == "helloWorld" 
	 * 		toCapitalizeCamelCase("hello_world") == "HelloWorld"
	 * 		toUnderScoreCase("helloWorld") = "hello_world"
	 */
    public static String toUnderScoreCase(String s) {
        if (s == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
    	boolean wordstart=false; //去除_开头的word开始
    	boolean wordHead=false; //是否处理了头
        for(int i=0;i<s.length();i++){
        	char c = s.charAt(i);
        	if(!wordstart){
        		if(c!='_'){
        			wordstart=true;
        		}else{
        			continue;
        		}
        	}
        	if(!wordHead){
        		char _c = Character.toLowerCase(c);
        		sb.append(_c);
        		wordHead=true;
        	}else{
            	if(Character.isUpperCase(c)){
            		char _c = Character.toLowerCase(c);
            		sb.append('_').append(_c);
            	}else{
            		sb.append(c);
            	}
        	}

        }
        
        
/*        StringBuilder sb = new StringBuilder();
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            boolean nextUpperCase = true;

            if (i < (s.length() - 1)) {
                nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
            }

            if ((i > 0) && Character.isUpperCase(c)) {
                if (!upperCase || !nextUpperCase) {
                    sb.append(SEPARATOR);
                }
                upperCase = true;
            } else {
                upperCase = false;
            }

            sb.append(Character.toLowerCase(c));
        }
*/
        return sb.toString();
    }
    /**
     * 能增加引号的join
     * @param arr
     * @param splitStr
     * @param quoteStr
     * @return
     */
    public static <T> String join(T[] arr,String splitStr,String quoteStr){
    	if(quoteStr==null){
    		return join(arr,splitStr);
    	}else{
    		List<String> newList = new ArrayList<String>();
    		for(T t : arr){
    			newList.add(quoteStr+t+quoteStr);
    		}
    		return join(newList,splitStr);
    	}
    }
}
